<template>
  <div class="login-page">
    <div class="login-header">
      <h2 class="login-title">欢迎回来</h2>
      <p class="login-subtitle">登录您的零码智绘账户</p>
    </div>

    <a-form
      :model="formState"
      name="basic"
      autocomplete="off"
      @finish="handleSubmit"
      class="login-form"
      layout="vertical"
    >
      <a-form-item
        label="账号"
        name="userAccount"
        :rules="[{ required: true, message: '请输入账号' }]"
      >
        <a-input
          v-model:value="formState.userAccount"
          placeholder="请输入您的账号"
          size="large"
          class="custom-input"
        >
          <template #prefix>
            <UserOutlined class="input-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        label="密码"
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码长度不能小于 8 位' },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          placeholder="请输入您的密码"
          size="large"
          class="custom-input"
        >
          <template #prefix>
            <LockOutlined class="input-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item>
        <a-button
          type="primary"
          html-type="submit"
          size="large"
          class="login-button"
          :loading="loading"
        >
          <span v-if="!loading">立即登录</span>
          <span v-else>登录中...</span>
        </a-button>
      </a-form-item>
    </a-form>

    <div class="login-footer">
      <div class="register-tip">
        还没有账号？
        <RouterLink to="/user/register" class="register-link"> 立即注册 </RouterLink>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { userLogin } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})

const router = useRouter()
const route = useRoute()
const loginUserStore = useLoginUserStore()
const loading = ref(false)

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: API.UserLoginRequest) => {
  loading.value = true
  try {
    const res = await userLogin(values)
    // 登录成功，把登录态保存到全局状态中
    if (res.data.code === 0 && res.data.data) {
      await loginUserStore.fetchLoginUser()
      message.success('登录成功，欢迎回来！')
      // 获取重定向地址，如果没有则默认跳转到首页
      const redirectPath = (route.query.redirect as string) || '/'
      await router.push({
        path: redirectPath,
        replace: true,
      })
    } else {
      message.error('登录失败：' + res.data.message)
    }
  } catch {
    message.error('登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-page {
  width: 100%;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #262626;
  margin: 0 0 8px;
  background: linear-gradient(135deg, #1890ff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  font-size: 16px;
  color: #8c8c8c;
  margin: 0;
  font-weight: 400;
}

.login-form {
  margin-bottom: 24px;
}

.login-form :deep(.ant-form-item-label > label) {
  font-weight: 600;
  color: #262626;
}

.login-form :deep(.ant-form-item-required::before) {
  display: none !important;
}

.custom-input {
  border-radius: 8px;
  border: 2px solid #f0f0f0;
  transition: all 0.3s ease;
}

.custom-input:hover,
.custom-input:focus-within {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
}

.input-icon {
  color: #8c8c8c;
  font-size: 16px;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  background: linear-gradient(135deg, #1890ff, #722ed1);
  border: none;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(135deg, #40a9ff, #9254de);
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(24, 144, 255, 0.4);
}

.login-footer {
  text-align: center;
}

.register-tip {
  color: #8c8c8c;
  font-size: 14px;
  margin-bottom: 24px;
}

.register-link {
  color: #1890ff;
  font-weight: 600;
  text-decoration: none;
  transition: all 0.3s ease;
}

.register-link:hover {
  color: #40a9ff;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-title {
    font-size: 24px;
  }

  .login-subtitle {
    font-size: 14px;
  }

  .login-button {
    height: 44px;
    font-size: 15px;
  }
}
</style>
